<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close
		@closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="100px"
			label-position="left">
			<el-row :gutter="24">
				<el-col :lg="24">
					<el-form-item label="策略名称" prop="notes_title">
						<el-input v-model="form.notes_title" placeholder="策略名称" clearable maxlength="180"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="24">
					<el-form-item label="招商空间" prop="room">
						<el-cascader v-model="form.space_id" :options="roomList"
							:props="{ expandTrigger: 'hover', value: 'id', label: 'room', children: 'spaceData' }"
							@change="handleChange"></el-cascader>
					</el-form-item>
				</el-col>
				<!-- <el-col :lg="24">
					<el-form-item label="招商策略" prop="ai_note">
						<el-input v-model="form.ai_note" placeholder="招商策略" clearable maxlength="40"></el-input>
					</el-form-item>
				</el-col> -->
				<el-col :lg="24">
					<el-form-item label="提案人" prop="user_role">
						<select-user-dialog v-model:accountId="form.user_role" :mode="true" />
					</el-form-item>
				</el-col>
				<el-col :lg="24">
					<el-form-item label="提案时间" prop="update_time">
						<el-date-picker v-model="form.update_time" :disabled-date="publishDateAfter" placeholder="提案时间"
							type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="话术建议" prop="content">
				<sc-editor v-model="form.content" placeholder="请输入" :templates="templates" :height="200" clearable
					style="width: 100%;"></sc-editor>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">确 定</el-button>
		</template>
	</el-dialog>
</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import selectDeptDialog from "@/views/module/selectDeptDialog";
import selectUserLevelDialog from "@/views/module/selectUserLevelDialog";
import { defineAsyncComponent } from "vue";
import msgBoxCheckBox from "@/views/module/msgBoxCheckBox";
import AttachMenu from '@/views/module/attachMenu'
import { tickStep } from "d3";
const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
export default {
	emits: ['success', 'closed'],
	components: {
		selectUserLevelDialog,
		msgBoxCheckBox,
		selectUserDialog,
		selectDeptDialog,
		AttachMenu,
		scEditor
	},
	data() {
		return {
			origin_building_id: 0,
			visible: false,
			isSaveing: false,
			isShowMsg: false,
			floorList: [],
			roomList: [],
			deviceList: [],
			mode: "add",
			titleMap: {
				add: '添加招商策略',
				edit: '编辑招商策略',
				show: '查看招商策略'
			},
			fileList: [],
			apiObj: this.$API.file.attach.uploadFile,
			templates: [],
			form2: {},
			form: {
				meetingId: '',
				notes_title: '',
				dbcode: '',
				content: '',
				chair: '',
				notesUser: '',
				roomId: '',
				beginTime: '',
				endTime: '',
				userJoin: '',
				deptJoin: '',
				levelJoin: '',
				otherJoin: '',
				content: '',
				msgType: '',
				attachId: '',
				id: '',
				note_id:'',
				space_id:[],
			},
			formSpace: {},
			formPackage: {},
			formPS: {},
			rules: {
				notes_title: [
					{ required: true, message: '招商策略标题不能为空', trigger: 'blur' }
				]
			},
			condition: true
		}
	},
	mounted() {


	},
	methods: {
		publishDateAfter(time) {
			return time.getTime() < (Date.now() - 24 * 60 * 60 * 1000)
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						// // 添加空间（名称及面积）
						// this.formSpace.building_id = 12
						// this.formSpace.category = 2
						// const resSpace = await this.$API.attractinvestment.space.insertSpace.post(this.formSpace);
						// // 添加包（包名）
						// this.formPackage = {name:this.formSpace.room}
						// const resPackage = await this.$API.attractinvestment.package.insertPackage.post(this.formPackage);
						// // 添加关联（关联上述二者）
						// this.formPS = {
						// 	package_id: resPackage.data,
						// 	space_id: resSpace.data,
						// 	floor_id: this.formSpace.floor_id,
						// 	building_id: 12
						// }
						// const resPS = await this.$API.attractinvestment.ps.insertPS.post(this.formPS);
						// // 添加话术
						// this.form.package_id = resPackage.data
						// delete this.form.building_id
						// this.form.building_id = 12
						// this.form.space_id = resSpace.data
						this.form.space_id = this.form.space_id[1]
						res = await this.$API.attractinvestment.note.insertNote.post(this.form);
						await this.$API.attractinvestment.space.updateSpace.post({ id: this.form.space_id, category: 2 });
						// res = await this.$API.attractinvestment.note.newNote.post(this.form);
					} else if (this.mode == "edit") {
						this.form.space_id = this.form.space_id[1]
						res = await this.$API.attractinvestment.note.updateNote.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				}
			})
		},
		async getFloorList() {
			var res = await this.$API.attractinvestment.floor.getFloorList.get({ 'building_id': this.form.building_id });
			if (res.code == 200) {
				this.floorList = res.data.list;
			}
		},
		async getCanUseDeviceList() {
			var res = await this.$API.office.meeting.getCanUseDeviceList.get();
			if (res.code == 200) {
				this.deviceList = res.data;
			}
		},
		setCreateData(data, paramRoomList) {
			this.roomList = []
			this.form.building_id = data.building.id;
			this.roomList = paramRoomList;
			this.roomList.forEach(element => {
				element.room = element.name
				element.spaceData =  element.spaceData.filter(space => space.category != 4)
			});
			this.form.roomId = data.roomId;
			this.form.beginTime = data.beginTime;
			this.form.endTime = data.endTime;
		},
		setData(data, roomList) {
			this.form.id = data.id;
			this.form.note_id = data.note_id;
			this.roomList = roomList;
			this.form.meetingId = data.meetingId
			this.form.notes_title = data.notes_title
			this.form.dbcode = data.dbcode
			this.form.content = data.content
			this.form.chair = data.chair
			this.form.notesUser = data.notesUser
			this.form.roomId = data.roomId
			this.form.beginTime = data.beginTime
			this.form.endTime = data.endTime
			this.form.joinCal = data.joinCal
			this.form.attachRole = data.attachRole
			this.form.userJoin = data.userJoin
			this.form.deptJoin = data.deptJoin
			this.form.levelJoin = data.levelJoin
			this.form.otherJoin = data.otherJoin
			this.form.content = data.content
			this.form.msgType = data.msgType
			this.form.attachId = data.attachId
		},

		setData(data) {
			this.createRoomList(data.building_space_id, data.space_id)
			this.form.id = data.id;
			this.form.note_id = data.note_id
			// let spaceArr =[]
			// spaceArr.push({id: data.space_id, room: data.room})
			// this.form.space_id.push({id: data.building_space_id, room: data.floor + '层', spaceData: spaceArr})
			this.form.space_id = [data.dbcode, parseInt(data.space_id)]
			// this.form.space_id.label = [data.floor + '层', data.room]
			this.form.notes_title = data.notes_title
			this.form.content = data.content
			this.form.update_time = data.update_time
			this.form.user_role = data.user_role
			console.log(this.form)
		},
		addNewFile(res, file) {
			let tArr = this.form.attachId.split(",");
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		},

		async createRoomList(data, space_id) {
			let buildingIdParam;
			if(data==undefined || data==null) {
				buildingIdParam = 'searchAllSpace';
			}else {
				buildingIdParam = data
			}

			let allData = await this.$API.attractinvestment.building.getFloorSpaceByBuildingId.get({ building_id: buildingIdParam});
				if(allData.code == 200) {
					for (let i = 0; i < allData.data.length; i++) {
					this.roomList.push({
						id: allData.data[i].id,
						room: allData.data[i].floor + '层',
						spaceData: allData.data[i].spaceList.filter(space => space.category != 4  || space.id == space_id),
					});
				}
				
			
		}
		},


	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
